package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.domain.*;
import cn.wolfcode.wolf2w.query.QueryObject;
import cn.wolfcode.wolf2w.query.TravelQueryObject;
import cn.wolfcode.wolf2w.service.*;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/destinations")
public class DestinationController {

    @Autowired
    private IRegionService regionService;
    @Autowired
    private IDestinationService destinationService;
    @Autowired
    private IStrategyCatalogService strategyCatalogService;
    @Autowired
    private IStrategyService strategyService;
    @Autowired
    private ITravelService travelService;

    //插叙热门区域
    @GetMapping("/hotRegion")
    public Object queryHotRegion(){
        List<Region> list=regionService.queryHotRegion();
        return JsonResult.success(list);
    }

    //热门区域下挂载的目的地
    @GetMapping("/search")
    public Object search(String regionId){
        List<Destination> list=destinationService.queryDestByRegionForApi(regionId);
        return JsonResult.success(list);
    }

    //吐司
    @GetMapping("/toasts")
    public Object toasts(String destId){
        List<Destination> destinations = destinationService.queryToasts(destId);
        return JsonResult.success(destinations);
    }

    //点击量前三
    @GetMapping("/strategies")
    public Object strategies(String destId){
        List<Strategy> strategys = strategyService.queryByThree(destId);
        return JsonResult.success(strategys);
    }

    @GetMapping("/catalogs")
    public Object catalogs(String destId){
        //通过目的地id找出该目的地下的攻略目录
        List<StrategyCatalog> catalogs=strategyCatalogService.queryByCatalog(destId);
        ////新建一个集合，封装数据
        //List<StrategyCatalog> list=new ArrayList<>();
        //进行循环便利，每便利一次，将一行的数据对应封装到集合中
        for(StrategyCatalog catalog:catalogs){
            String id = catalog.getId();
            //通过攻略目录查找对应的攻略明细
            List<Strategy> sts=strategyService.queryByCatalogId(id);
            catalog.setStrategies(sts);

        }
        return JsonResult.success(catalogs);
    }

    //目的地明细-游记
    @GetMapping("/travels")
    public Object travels(TravelQueryObject qo){
        Page<Travel> page = travelService.query(qo);
        return JsonResult.success(page);
    }

    //查询目的地
    @GetMapping("/list")
    public Object list(){
        return JsonResult.success(destinationService.selectAll());
    }


}
